/**
 *  the right way of binary search;
 *	@author Kelvin Pan
 */
#include <iostream>
using namespace std;

/** 
 * @param num[] :number array;
 * @param n :the length of num[];
 * @param x :input value;
 * @return the first index >= x, if no element >= x return n
 * [l,r)
 */
int binary_search(int num[] , int n , int x) {
	int l = 0, r = n , mid;
	while(l < r){
		mid = l + (r - l) / 2;
		if(num[mid] >= x)
			r = mid;
		else
			l = mid + 1;
	}
	return l;
}

int main(int argc, const char *argv[])
{
	int a[] = {1,2,3};
	int n = sizeof(a)/sizeof(a[0]);
	int x,i;
	for (i = 0,cout<<"{"; i < n - 1 ; i++) {
		cout<<a[i]<<",";
	}
	cout<<a[i]<<"}\n\n";
	while(cin>>x){
		cout<<"\tresult:"<<binary_search(a,n,x)<<endl;
	}

	return 0;
}
